package com.cangxuan.app.ui.user;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.Toast;
import com.alibaba.fastjson.JSONObject;
import com.cangxuan.app.R;
import com.cangxuan.app.entity.User;
import com.cangxuan.app.ui.BaseActivity;
import com.cangxuan.app.utils.DeviceUtils;
import com.cangxuan.app.utils.HttpUtils;
import com.cangxuan.app.utils.UserUtils;

import java.net.HttpURLConnection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *  Author: 彭伟辰、田凯
 *  Activity: 隐私设置界面
 *  Layout: activity_set_privacy
 */

public class SetPrivacyActivity extends BaseActivity {
    Switch displayForSale;//是否对外展示出售的藏品
    Switch showOwnership;//
    Handler handler=new Handler();
    User user;
    Map<String,String> header=new HashMap<>();//请求头
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_privacy);
        setTitle("隐私设置");
        displayForSale=findViewById(R.id.displayForSale);
        showOwnership=findViewById(R.id.showOwnership);

        header.put("Authorization", DeviceUtils.getDeviceID());

        user= UserUtils.getUser(this);
        showOwnership.setChecked(user.isDisplayMyGoods());
        displayForSale.setChecked(user.isDisplayMySellGoods());

        showOwnership.setOnCheckedChangeListener((compoundButton, isChecked) -> {
            setPrivacy(isChecked,"/api/user/setPrivacyUpdateDisplayMySellGoods/",1);
        });
        displayForSale.setOnCheckedChangeListener((compoundButton, isChecked) -> {

            setPrivacy(isChecked,"/api/user/setPrivacyUpdateDisplayMySellGoods/",2);
        });
    }
    public void setPrivacy(boolean isChecked,String api,int id){
        ProgressDialog progressDialog = ProgressDialog.show(this, "", "请稍等...");
        Thread thread1=new Thread(() -> {
            String s = HttpUtils.get(api+(isChecked?1:0),header);
            JSONObject jsonResponse = JSONObject.parseObject(s);
            if(jsonResponse.getInteger("status")==HttpURLConnection.HTTP_OK){
                handler.post(() -> {
                    progressDialog.dismiss();
                    if(id==1){
                        user.setDisplayMyGoods(isChecked);
                    }else{
                        user.setDisplayMySellGoods(isChecked);
                    }

                    UserUtils.setUser(this,user);
                    Toast.makeText(this, jsonResponse.getString("message"), Toast.LENGTH_SHORT).show();

                });
            }else {

                handler.post(() -> {
                    progressDialog.dismiss();
                    if(id==1){
                        user.setDisplayMyGoods(isChecked);
                    }else{
                        user.setDisplayMySellGoods(isChecked);
                    }
                    UserUtils.setUser(this,user);
                    Toast.makeText(this, jsonResponse.getString("message"), Toast.LENGTH_SHORT).show();
                });
            }
        });
        thread1.start();
    }
}
